﻿using System;
using DataAccess.Models;
using DataAccess.Repository;

namespace KPI
{
	public partial class MailDetail : BasePage
	{
		private IMailRepository _MailRepository;

		public int TheMailID
		{
			get
			{
				return CommonHelper.QueryStringInt("id");
			}
		}

		public MailMessage TheMailMeessage
		{
			get
			{
				MailMessage result = null;

				if(TheMailID > 0)
				{
					if(_MailRepository == null)
					{
						_MailRepository = new MailRepository();
					}

					result = _MailRepository.GetTheMailMessage(TheMailID);
				}

				return result;
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			if(TheCurrentLoginUser == null)
			{
				Response.Redirect("~/Login.aspx");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindMailMessage();

				btnReply.Visible = !(TheCurrentLoginUser.UserName.Equals(TheMailMeessage.MailFrom)) && TheMailMeessage.MailIsDelete != 1;
			}
		}

		protected void btnReply_Click(object sender, EventArgs e)
		{
			Response.Redirect("~/WriteMail.aspx?mailid=" + TheMailID.ToString());
		}

		protected void btnBack_Click(object sender, EventArgs e)
		{
			var currentUserName = TheCurrentLoginUser != null ? TheCurrentLoginUser.UserName : string.Empty;

			if(!string.IsNullOrWhiteSpace(currentUserName) && TheMailMeessage.MailFrom.Equals(currentUserName))
			{
				Response.Redirect("~/MailOutbox.aspx");
			}

			Response.Redirect("~/MailInbox.aspx");
		}

		protected void btnDelete_Click(object sender, EventArgs e)
		{
			if(TheMailID > 0)
			{
				if(_MailRepository == null)
				{
					_MailRepository = new MailRepository();
				}

				if(TheMailMeessage != null)
				{
					TheMailMeessage.MailIsDelete = 1;

					var currentUserName = TheCurrentLoginUser.UserName;

					var lstIDs = new System.Collections.Generic.List<int>();
					lstIDs.Add(TheMailID);

					if(TheMailMeessage.MailFrom.Equals(currentUserName))
					{
						if(_MailRepository.DeleteOutboxMail(lstIDs))
						{
							Response.Redirect("~/MailOutbox.aspx");
						}
						else
						{
							ShowMessage("出错了, 请稍后再试或联系相关技术人员.", true);
						}
					}
					else
					{
						if(_MailRepository.DeleteInboxMail(lstIDs, currentUserName))
						{
							Response.Redirect("~/MailInbox.aspx");
						}
						else
						{
							ShowMessage("出错了, 请稍后再试或联系相关技术人员.", true);
						}
					}
				}
			}
		}

		protected void Page_UnLoad(object sender, EventArgs e)
		{
			var currentUserName = TheCurrentLoginUser.UserName;

			if(TheMailMeessage != null && !string.IsNullOrWhiteSpace(currentUserName) && !TheMailMeessage.MailFrom.Equals(currentUserName))
			{
				_MailRepository.SetRecievedMailIsRead(currentUserName, TheMailID, 1);
			}

			if(_MailRepository != null)
			{
				((IDisposable)_MailRepository).Dispose();
			}
		}

		private void BindMailMessage()
		{
			if(TheMailMeessage != null)
			{
				lMailSubject.Text = TheMailMeessage.MailSubject;
				lMailSender.Text = TheMailMeessage.MailFrom;
				lMailSentTime.Text = TheMailMeessage.MailSendTime.ToString("yyyy年MM月dd日 hh时mm分");
				lMailContent.Text = TheMailMeessage.MailContent;
				if(!string.IsNullOrWhiteSpace(TheMailMeessage.MailAttFile))
				{
					var theUrl = GetTheURL();

					hlMailAttFile.InnerText = System.IO.Path.GetFileName(TheMailMeessage.MailAttFile);
					//var ecd = System.Text.Encoding.GetEncoding("GB2312");
					//var fileName = ecd.GetBytes(TheMailMeessage.MailAttFile);

					var theAttFilePath = (string.IsNullOrWhiteSpace(theUrl) ? "/" : theUrl) + TheMailMeessage.MailAttFile;

					hlMailAttFile.HRef = theAttFilePath;
				}
			}
		}

		private string GetTheURL()
		{
			var result = string.Empty;

			string url = "http://" + Request.Url.Host;
			if(Request.Url.Port > 0)
			{
				url += ":" + Request.Url.Port.ToString();
			}

			if(!url.EndsWith("/"))
			{
				result = url + "/";
			}

			return result;
		}
	}
}